<?php


namespace App\Services;


use App\Contacts\Exp;
use GuzzleHttp\Client;

class JuHeExp implements Exp
{
    protected $appKey;

    public function __construct()
    {
        $this->appKey = config('exp.juhe.appKey');
    }

    public function information(string $code, string $com): array
    {
        $data = [
            'com' => $com,
            'no' => $code,
            'key' => $this->appKey,
        ];

        $client = new Client;

        $response = $client->post('http://v.juhe.cn/exp/index?' . http_build_query($data));

        $result = json_decode((string)$response->getBody(), true);

        if ($result) {
            $list = $result['result']['list'];

            return array_reverse($list);
        }

        return [];
    }

    /**
     * @return array|mixed
     */
    public function getAllExpCom(): array
    {
        $client = new Client();

        $response = $client->post('http://v.juhe.cn/exp/com?key=' . $this->appKey);

        return json_decode((string)$response->getBody(), true);
    }
}
